﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FisApp.enums;
using FisApp.utility;
using System.Data.SqlClient;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace FisApp
{
    public partial class FrmFis : Form
    {
        #region Variables...
        private string fisId;
        private string sqlCommandType = SqlCommandType.INSERT;
        string imageName;
        string imagePath;

        public int? kalite
        {
            get
            {
                return (int?)kaliteLookUp.EditValue;
            }
            set
            {
                kaliteLookUp.EditValue = value;
            }
        }

        public int? taban
        {
            get
            {
                return (int?)tabanLookUp.EditValue;
            }
            set
            {
                tabanLookUp.EditValue = value;
            }
        }

        public int? renk
        {
            get
            {
                return (int?)renkLookUp.EditValue;
            }
            set
            {
                renkLookUp.EditValue = value;
            }
        }

        public int? deri
        {
            get
            {
                return (int?)deriLookUp.EditValue;
            }
            set
            {
                deriLookUp.EditValue = value;
            }
        }

        public int? astar
        {
            get
            {
                return (int?)astarLookUp.EditValue;
            }
            set
            {
                astarLookUp.EditValue = value;
            }
        }

        public int? lazer
        {
            get
            {
                return (int?)lazerLookUp.EditValue;
            }
            set
            {
                lazerLookUp.EditValue = value;
            }
        }
        
        public int? elSarac
        {
            get
            {
                return (int?)elSaracLookUp.EditValue;
            }
            set
            {
                elSaracLookUp.EditValue = value;
            }
        }

        public string elSaracAciklama
        {
            get
            {
                return txtelSaracAciklama.Text;
            }
            set
            {
                txtelSaracAciklama.Text = value;
            }
        }

        public int? makineSarac
        {
            get
            {
                return (int?)makineSaracLookUp.EditValue;
            }
            set
            {
                makineSaracLookUp.EditValue = value;
            }
        }

        public string makineSaracAciklama
        {
            get
            {
                return txtMakineSaracAciklama.Text;
            }
            set
            {
                txtMakineSaracAciklama.Text = value;
            }
        }

        public Bitmap resim
        {
            get
            {
                return (Bitmap)peResim.EditValue;
            }
            set
            {
                peResim.EditValue = value;
            }
        }

        public bool acil
        {
            get
            {
                return ceAcil.Checked; 
            }
            set
            {
                ceAcil.Checked = value;
            }
        }

        public DataTable Fisler
        {
            get
            {
                return (DataTable)gcFis.DataSource;
            }

            set
            {
                gcFis.DataSource = value;
            }
        }

        public DataRow SelectedRow
        {
            get
            {
                return gvFis.GetDataRow(gvFis.FocusedRowHandle);
            }
        }
        #endregion
        
        #region Constructor...
        public FrmFis()
        {
            InitializeComponent();
        }
        #endregion

        #region Methods...
        private void clearForm()
        {
            fisId = null;
            kalite = null;
            taban = null;
            renk = null;
            deri = null;
            astar = null;
            lazer = null;
            resim = null;
            elSarac = null;
            elSaracAciklama = null;
            makineSarac = null;
            makineSaracAciklama = null;
            
            imageName = null;
            txtSiparis.Text = null;
            deTeslimTarih.EditValue = null;
            txt39.Text = null;
            txt40.Text = null;
            txt41.Text = null;
            txt42.Text = null;
            txt43.Text = null;
            txt44.Text = null;
            txt45.Text = null;
            acil = false;

            btnVazgec.Visible = false;
            sqlCommandType = SqlCommandType.INSERT;
        }

        private void LoadFisler()
        {
            try
            {
                string query = @"SELECT * FROM fis m 
                                   JOIN kalite k ON k.kalite_id = m.kalite_id
                                   JOIN taban t ON t.taban_id = m.taban_id
                                   JOIN renk r ON r.renk_id = m.renk_id
                                   JOIN deri d ON d.deri_id = m.deri_id
                              LEFT JOIN astar a ON a.astar_id = m.astar_id
                              LEFT JOIN lazer l ON l.lazer_id = m.lazer_id
                              LEFT JOIN elsarac e ON e.elsarac_id = m.elsarac_id
                              LEFT JOIN makinesarac ms ON ms.makinesarac_id = m.makinesarac_id";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                    Fisler = ds.Tables[0];

                gvFis.BestFitColumns();
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void ModeliFiseAktar(int? modelId)
        {
            if (modelId == null)
                return;

            string query = @"SELECT * FROM model m 
                                   JOIN kalite k ON k.kalite_id = m.kalite_id
                                   JOIN taban t ON t.taban_id = m.taban_id
                                   JOIN renk r ON r.renk_id = m.renk_id
                                   JOIN deri d ON d.deri_id = m.deri_id
                              LEFT JOIN astar a ON a.astar_id = m.astar_id
                              LEFT JOIN lazer l ON l.lazer_id = m.lazer_id
                              LEFT JOIN elsarac e ON e.elsarac_id = m.elsarac_id
                              LEFT JOIN makinesarac ms ON ms.makinesarac_id = m.makinesarac_id
                                  WHERE model_id=@model_id";

            List<SqlParameter> list = new List<SqlParameter>();
            list.Add(DBUtils.GetParameter("@model_id", modelId));

            DataSet ds = DBUtils.SQLSelect(query, list);

            if (ds == null)
                return;

            if (ds.Tables[0].Rows.Count != 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                kalite= Convert.ToInt32(dr["kalite_id"]);
                LoadTaban();
                taban = Convert.ToInt32(dr["taban_id"]);
                renk = Convert.ToInt32(dr["renk_id"]);
                deri = Convert.ToInt32(dr["deri_id"]);
                astar = (int?)(dr["astar_id"].Equals(DBNull.Value) ? null : dr["astar_id"]);
                lazer = (int?)(dr["lazer_id"].Equals(DBNull.Value) ? null : dr["lazer_id"]);
                elSarac = (int?)(dr["elsarac_id"].Equals(DBNull.Value) ? null : dr["elsarac_id"]);
                makineSarac = (int?)(dr["makinesarac_id"].Equals(DBNull.Value) ? null : dr["makinesarac_id"]);
                imageName = dr["image_name"].ToString();
                imagePath = dr["image_path"].ToString();
                try
                {
                    resim = new Bitmap(dr["image_path"].ToString());
                }
                catch (Exception ex)
                {
                    resim = FisApp.Properties.Resources.not_found;
                }
                //txtTabanMarkasi.Text = SelectedRow["taban_marka"].ToString();
                //txtTabanNo.Text = SelectedRow["taban_no"].ToString();
                //modelId = dr["model_id"].ToString();
                //sqlCommandType = SqlCommandType.UPDATE;
                //btnVazgec.Visible = true;
            }
        }

        private void LoadKalite()
        {
            try
            {
                string query = "SELECT k.*, kalite_no + ' - ' + kalip_no as kalite_kalip FROM kalite k";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                {
                    kaliteLookUp.Properties.DataSource = ds.Tables[0];
                    kaliteLookUp.Properties.DisplayMember = "kalite_kalip";
                    kaliteLookUp.Properties.ValueMember = "kalite_id";
                }
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void LoadTaban()
        {
            try
            {
                string query = "SELECT t.*, taban_marka + ' - ' + taban_no +' - '+ taban_renk as taban_marka_no_renk  FROM taban t WHERE kalite_id=@kalite_id";

                List<SqlParameter> list = new List<SqlParameter>();
                list.Add(DBUtils.GetParameter("@kalite_id", kalite));
                DataSet ds = DBUtils.SQLSelect(query, list);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                {
                    tabanLookUp.Properties.DataSource = ds.Tables[0];
                    tabanLookUp.Properties.DisplayMember = "taban_marka_no_renk";
                    tabanLookUp.Properties.ValueMember = "taban_id";
                }
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void LoadRenk()
        {
            try
            {
                string query = "SELECT r.*, renk_kodu + ' - ' + renk_aciklama as renk_kod_aciklama  FROM renk r";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                {
                    renkLookUp.Properties.DataSource = ds.Tables[0];
                    renkLookUp.Properties.DisplayMember = "renk_kod_aciklama";
                    renkLookUp.Properties.ValueMember = "renk_id";
                }
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void LoadDeri()
        {
            try
            {
                string query = "SELECT d.*, deri_kodu + ' - ' + deri_aciklama as deri_kod_aciklama FROM deri d";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                {
                    deriLookUp.Properties.DataSource = ds.Tables[0];
                    deriLookUp.Properties.DisplayMember = "deri_kod_aciklama";
                    deriLookUp.Properties.ValueMember = "deri_id";
                }
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void LoadAstar()
        {
            try
            {
                string query = "SELECT a.*, astar_kodu + ' - ' + astar_aciklama as astar_kod_aciklama FROM astar a";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                {
                    astarLookUp.Properties.DataSource = ds.Tables[0];
                    astarLookUp.Properties.DisplayMember = "astar_kod_aciklama";
                    astarLookUp.Properties.ValueMember = "astar_id";
                }
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void LoadLazer()
        {
            try
            {
                string query = "SELECT l.*, lazer_kodu + ' - ' + lazer_aciklama as lazer_kod_aciklama FROM lazer l";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                {
                    lazerLookUp.Properties.DataSource = ds.Tables[0];
                    lazerLookUp.Properties.DisplayMember = "lazer_kod_aciklama";
                    lazerLookUp.Properties.ValueMember = "lazer_id";
                }
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void LoadElSarac()
        {
            try
            {
                string query = "SELECT e.*, elsarac_kodu + ' - ' + elsarac_aciklama as elsarac_kod_aciklama  FROM elsarac e";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                {
                    elSaracLookUp.Properties.DataSource = ds.Tables[0];
                    elSaracLookUp.Properties.DisplayMember = "elsarac_kod_aciklama";
                    elSaracLookUp.Properties.ValueMember = "elsarac_id";
                }
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void LoadMakineSarac()
        {
            try
            {
                string query = "SELECT m.*, makinesarac_kodu + ' - ' + makinesarac_aciklama as makinesarac_kod_aciklama FROM makinesarac m";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                {
                    makineSaracLookUp.Properties.DataSource = ds.Tables[0];
                    makineSaracLookUp.Properties.DisplayMember = "makinesarac_kod_aciklama";
                    makineSaracLookUp.Properties.ValueMember = "makinesarac_id";
                }
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }
        #endregion

        #region Events...
        private void FrmFis_Load(object sender, EventArgs e)
        {
            LoadKalite();
            LoadTaban();
            LoadRenk();
            LoadDeri();
            LoadAstar();
            LoadLazer();
            LoadElSarac();
            LoadMakineSarac();
            LoadFisler();
        }

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSiparis.Text))
            {
                FrmMessage.showDialog("Lütfen sipariş bilgisini giriniz.", MessageTypes.Warning);
                return;
            }
            if (kalite == null)
            {
                FrmMessage.showDialog("Lütfen kalite bilgisini giriniz.", MessageTypes.Warning);
                return;
            }
            if (taban == null)
            {
                FrmMessage.showDialog("Lütfen taban bilgisini giriniz.", MessageTypes.Warning);
                return;
            }
            if (deri == null)
            {
                FrmMessage.showDialog("Lütfen deri bilgisini giriniz.", MessageTypes.Warning);
                return;
            }
            if (renk == null)
            {
                FrmMessage.showDialog("Lütfen renk bilgisini giriniz.", MessageTypes.Warning);
                return;
            }
            if (deTeslimTarih.EditValue == null)
            {
                FrmMessage.showDialog("Lütfen teslim tarihini giriniz.", MessageTypes.Warning);
                return;
            }

            try
            {
                //string imagePath = Application.StartupPath + "\\Image\\";

                //if (!System.IO.File.Exists(imagePath))
                //{
                //    System.IO.Directory.CreateDirectory(imagePath);
                //}

                //string[] strImage = imageName.Split('.');
                //string newImageName = strImage[0] + "_" + String.Format("{0:yyyyMMddHHmmss}", System.DateTime.Now) + "." + strImage[1];
                //imagePath += newImageName;
                int fis_id = 0;
                if (sqlCommandType.Equals(SqlCommandType.INSERT))
                {
                    string str = "kalite_id,taban_id,renk_id,deri_id,astar_id,lazer_id,elsarac_id,elsarac_aciklama,makinesarac_id,makinesarac_aciklama,siparis,teslim_tarih,siparis_tarih,_39,_40,_41,_42,_43,_44,_45,image_name,image_path,acil";
                    string str2 = "@kalite_id,@taban_id,@renk_id,@deri_id,@astar_id,@lazer_id,@elsarac_id,@elsarac_aciklama,@makinesarac_id,@makinesarac_aciklama,@siparis,@teslim_tarih,@siparis_tarih,@_39,@_40,@_41,@_42,@_43,@_44,@_45,@image_name,@image_path,@acil";
                    //string str2 = kalite + "," + taban + "," + renk + "," + deri + ",'" + txtSiparis.Text + "','" + DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + "','" + DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss")+"'";
                    string command = "INSERT INTO fis (" + str + ") VALUES (" + str2 + ")";

                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@kalite_id", kalite));
                    list.Add(DBUtils.GetParameter("@taban_id", taban));
                    list.Add(DBUtils.GetParameter("@renk_id", renk));
                    list.Add(DBUtils.GetParameter("@deri_id", deri));
                    list.Add(DBUtils.GetParameter("@astar_id", astar));
                    list.Add(DBUtils.GetParameter("@lazer_id", lazer));
                    list.Add(DBUtils.GetParameter("@elsarac_id", elSarac));
                    list.Add(DBUtils.GetParameter("@elsarac_aciklama", txtelSaracAciklama.Text));
                    list.Add(DBUtils.GetParameter("@makinesarac_id", makineSarac));
                    list.Add(DBUtils.GetParameter("@makinesarac_aciklama", txtMakineSaracAciklama.Text)); 
                    list.Add(DBUtils.GetParameter("@siparis", txtSiparis.Text));
                    list.Add(DBUtils.GetParameter("@teslim_tarih", deTeslimTarih.DateTime.ToString("yyyy/MM/dd hh:mm:ss")));
                    list.Add(DBUtils.GetParameter("@siparis_tarih", DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss")));
                    list.Add(DBUtils.GetParameter("@_39", txt39.Text));
                    list.Add(DBUtils.GetParameter("@_40", txt40.Text));
                    list.Add(DBUtils.GetParameter("@_41", txt41.Text));
                    list.Add(DBUtils.GetParameter("@_42", txt42.Text));
                    list.Add(DBUtils.GetParameter("@_43", txt43.Text));
                    list.Add(DBUtils.GetParameter("@_44", txt44.Text));
                    list.Add(DBUtils.GetParameter("@_45", txt45.Text));
                    list.Add(DBUtils.GetParameter("@image_name", imageName));
                    list.Add(DBUtils.GetParameter("@image_path", imagePath));
                    list.Add(DBUtils.GetParameter("@acil", acil));

                    object obj = DBUtils.ExecuteSQLCommand(command, list);
                    fis_id = Convert.ToInt32(obj);
                    
                }
                else if (sqlCommandType.Equals(SqlCommandType.UPDATE))
                {
                    string str = "kalite_id=@kalite_id,taban_id=@taban_id,renk_id=@renk_id,deri_id=@deri_id,astar_id=@astar_id,lazer_id=@lazer_id,elsarac_id=@elsarac_id,elsarac_aciklama=@elsarac_aciklama,makinesarac_id=@makinesarac_id,makinesarac_aciklama=@makinesarac_aciklama,siparis=@siparis,teslim_tarih=@teslim_tarih,siparis_tarih=@siparis_tarih,_39=@_39,_40=@_40,_41=@_41,_42=@_42,_43=@_43,_44=@_44,_45=@_45,image_name=@image_name,image_path=@image_path,acil=@acil";
                    string command = "UPDATE fis SET " + str + " WHERE fis_id = @fis_id";

                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@kalite_id", kalite));
                    list.Add(DBUtils.GetParameter("@taban_id", taban));
                    list.Add(DBUtils.GetParameter("@renk_id", renk));
                    list.Add(DBUtils.GetParameter("@deri_id", deri));
                    list.Add(DBUtils.GetParameter("@astar_id", astar));
                    list.Add(DBUtils.GetParameter("@lazer_id", lazer));
                    list.Add(DBUtils.GetParameter("@elsarac_id", elSarac));
                    list.Add(DBUtils.GetParameter("@elsarac_aciklama", txtelSaracAciklama.Text));
                    list.Add(DBUtils.GetParameter("@makinesarac_id", makineSarac));
                    list.Add(DBUtils.GetParameter("@makinesarac_aciklama", txtMakineSaracAciklama.Text));
                    list.Add(DBUtils.GetParameter("@siparis", txtSiparis.Text));
                    list.Add(DBUtils.GetParameter("@teslim_tarih", deTeslimTarih.DateTime.ToString("yyyy/MM/dd hh:mm:ss")));
                    list.Add(DBUtils.GetParameter("@siparis_tarih", DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss")));
                    list.Add(DBUtils.GetParameter("@_39", txt39.Text));
                    list.Add(DBUtils.GetParameter("@_40", txt40.Text));
                    list.Add(DBUtils.GetParameter("@_41", txt41.Text));
                    list.Add(DBUtils.GetParameter("@_42", txt42.Text));
                    list.Add(DBUtils.GetParameter("@_43", txt43.Text));
                    list.Add(DBUtils.GetParameter("@_44", txt44.Text));
                    list.Add(DBUtils.GetParameter("@_45", txt45.Text));
                    list.Add(DBUtils.GetParameter("@image_name", imageName));
                    list.Add(DBUtils.GetParameter("@image_path", imagePath));
                    list.Add(DBUtils.GetParameter("@acil", acil));
                    list.Add(DBUtils.GetParameter("@fis_id", fisId));

                    DBUtils.ExecuteSQLCommand(command, list);
                    fis_id = Convert.ToInt32(fisId);
                }

                if (ceYazdir.Checked && fis_id != 0)
                {
                    Reports.ReportViewer rp = new Reports.ReportViewer();
                    rp.ShowFis(fis_id);
                }

                LoadFisler();
                clearForm();
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Kayıt işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void repbtnEdit_Click(object sender, EventArgs e)
        {
            fisId = SelectedRow["fis_id"].ToString();
            kalite = Convert.ToInt32(SelectedRow["kalite_id"]);
            LoadTaban();
            taban = Convert.ToInt32(SelectedRow["taban_id"]);
            renk = Convert.ToInt32(SelectedRow["renk_id"]);
            deri = Convert.ToInt32(SelectedRow["deri_id"]);
            astar = (int?)(SelectedRow["astar_id"].Equals(DBNull.Value) ? null : SelectedRow["astar_id"]);
            lazer = (int?)(SelectedRow["lazer_id"].Equals(DBNull.Value) ? null : SelectedRow["lazer_id"]);
            elSarac = (int?)(SelectedRow["elsarac_id"].Equals(DBNull.Value) ? null : SelectedRow["elsarac_id"]);
            makineSarac = (int?)(SelectedRow["makinesarac_id"].Equals(DBNull.Value) ? null : SelectedRow["makinesarac_id"]);
            imageName = SelectedRow["image_name"].ToString();
            imagePath = SelectedRow["image_path"].ToString();
            try
            {
                resim = new Bitmap(SelectedRow["image_path"].ToString());
            }catch(Exception ex){
                resim = FisApp.Properties.Resources.not_found;
            }

            elSaracAciklama = SelectedRow["elsarac_aciklama"].ToString();
            makineSaracAciklama = SelectedRow["makinesarac_aciklama"].ToString();
            txtSiparis.Text = SelectedRow["siparis"].ToString();
            deTeslimTarih.DateTime = Convert.ToDateTime(SelectedRow["teslim_tarih"]);
            txt39.Text = SelectedRow["_39"].ToString();
            txt40.Text = SelectedRow["_40"].ToString();
            txt41.Text = SelectedRow["_41"].ToString();
            txt42.Text = SelectedRow["_42"].ToString();
            txt43.Text = SelectedRow["_43"].ToString();
            txt44.Text = SelectedRow["_44"].ToString();
            txt45.Text = SelectedRow["_45"].ToString();
            acil = Convert.ToBoolean(SelectedRow["acil"]);
            sqlCommandType = SqlCommandType.UPDATE;
            btnVazgec.Visible = true;
        }

        private void repbtnDelete_Click(object sender, EventArgs e)
        {
            fisId = SelectedRow["fis_id"].ToString();

            try
            {
                if (FrmMessage.ShowDialogResult(SelectedRow["fis_id"].ToString().PadLeft(5, '0') + " no'lu fis bilgisini silmek istediğinize emin misiniz ?", MessageTypes.Question) == DialogResult.Yes)
                {
                    string command = "DELETE FROM fis WHERE fis_id=@fis_id";
                   
                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@fis_id", fisId));

                    DBUtils.ExecuteSQLCommand(command, list);

                    LoadFisler();
                    clearForm();
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.ToString().Contains("-2146232060"))
                    FrmMessage.showDialog(General.ForeignKeyExceptionMessage, MessageTypes.Error);
                else
                    FrmMessage.showDialog("Silme işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);

                //FrmMessage.showDialog("Silme işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void btnVazgec_Click(object sender, EventArgs e)
        {
            clearForm();
        }

        private void tabanLookUp_QueryPopUp(object sender, CancelEventArgs e)
        {
            if (kalite == null)
            {
                FrmMessage.showDialog("Önce kalite seçiniz...", MessageTypes.Information);
                return;
            }
            LoadTaban();
        }

        private void FrmFis_Activated(object sender, EventArgs e)
        {
            FrmFis_Load(null, null);
        }

        private void gvModel_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.Column.FieldName == "Resim" && e.IsGetData)
            {
                try
                {
                    e.Value = new Bitmap((string)((DataRowView)e.Row)["image_path"]);// "";// imageCollection1.Images(0);
                }catch(Exception ex){
                    e.Value = FisApp.Properties.Resources.not_found;
                }
            }

            if (e.Column.FieldName == "FisNo" && e.IsGetData)
            {
                e.Value = ((DataRowView)e.Row)["fis_id"].ToString().PadLeft(5, '0');// new Bitmap();// "";// imageCollection1.Images(0);
            }
        }

        private void btnModelSec_Click(object sender, EventArgs e)
        {
            FrmModelImages frmModelImages = new FrmModelImages();
            frmModelImages.ShowDialog();
            int? modelId = frmModelImages.modelId;
            ModeliFiseAktar(modelId);
        }
        #endregion

        private void btnTemizle_Click(object sender, EventArgs e)
        {
            clearForm();
        }

        private void SelectModel()
        {

            try
            {
                if (kalite == null || taban == null || deri == null || renk == null)
                    return;

            
                string query = @"SELECT * FROM model m 
                                      WHERE kalite_id = @kalite_id AND taban_id = @taban_id AND deri_id = @deri_id AND renk_id = @renk_id";

                List<SqlParameter> list = new List<SqlParameter>();

                list.Add(DBUtils.GetParameter("@kalite_id", kalite));
                list.Add(DBUtils.GetParameter("@taban_id", taban));
                list.Add(DBUtils.GetParameter("@deri_id", deri));
                list.Add(DBUtils.GetParameter("@renk_id", renk));

                //if (kalite != null)
                //{
                //    query += " and kalite_id = @kalite_id";
                //    list.Add(DBUtils.GetParameter("@kalite_id", kalite));
                //}

                //if (taban != null)
                //{
                //    query += " and taban_id = @taban_id";
                //    list.Add(DBUtils.GetParameter("@taban_id", taban));
                //}

                //if (deri != null)
                //{
                //    query += " and deri_id = @deri_id";
                //    list.Add(DBUtils.GetParameter("@deri_id", deri));
                //}

                //if (renk != null)
                //{
                //    query += " and renk_id = @renk_id";
                //    list.Add(DBUtils.GetParameter("@renk_id", renk));
                //}

                if (astar != null)
                {
                    query += " and astar_id = @astar_id";
                    list.Add(DBUtils.GetParameter("@astar_id", astar));
                }

                if (lazer != null)
                {
                    query += " and lazer_id = @lazer_id";
                    list.Add(DBUtils.GetParameter("@lazer_id", lazer));
                }

                if (elSarac != null)
                {
                    query += " and elSarac_id = @elSarac_id";
                    list.Add(DBUtils.GetParameter("@elSarac_id", elSarac));
                }

                if (makineSarac != null)
                {
                    query += " and makineSarac_id = @makineSarac_id";
                    list.Add(DBUtils.GetParameter("@makineSarac_id", makineSarac));
                }

                imageName = null;
                imagePath = null;
                resim = null;

                DataSet ds = DBUtils.SQLSelect(query, list);

                if (ds == null)
                    return;

                if (ds.Tables[0].Rows.Count == 1)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    
                    imageName = dr["image_name"].ToString();
                    imagePath = dr["image_path"].ToString();
                    try
                    {
                        resim = new Bitmap(dr["image_path"].ToString());
                    }
                    catch (Exception ex)
                    {
                        resim = FisApp.Properties.Resources.not_found;
                    }
                }
                
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Silme işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void kaliteLookUp_EditValueChanged(object sender, EventArgs e)
        {
            SelectModel();
        }

        private void tabanLookUp_EditValueChanged(object sender, EventArgs e)
        {
            SelectModel();
        }

        private void deriLookUp_EditValueChanged(object sender, EventArgs e)
        {
            SelectModel();
        }

        private void renkLookUp_EditValueChanged(object sender, EventArgs e)
        {
            SelectModel();
        }

        private void astarLookUp_EditValueChanged(object sender, EventArgs e)
        {
            SelectModel();
        }

        private void lazerLookUp_EditValueChanged(object sender, EventArgs e)
        {
            SelectModel();
        }

        private void elSaracLookUp_EditValueChanged(object sender, EventArgs e)
        {
            SelectModel();
        }

        private void makineSaracLookUp_EditValueChanged(object sender, EventArgs e)
        {
            SelectModel();
        }

    }
}
